#include "httplib.h"
#include "searcher.hpp"
#include "log.hpp"

const string rawPath = "./data/raw_html/raw.txt";
const char *base_dir = "./wwwroot";
int main(void)
{
    ns_searcher::Searcher searcher;
    searcher.InitSearcher(rawPath);
    LOG(NORMAL, "服务器启动完毕！");
    httplib::Server svr;
    svr.set_base_dir(base_dir);
    svr.Get("/s", [&](const httplib::Request &req, httplib::Response &rsp)
            {
                if (!req.has_param("word"))
                {
                    rsp.set_content("必须要有搜索关键字！", "text/plain;charset=utf-8");
                    return;
                }
                string word = req.get_param_value("word");
                LOG(NORMAL, "用户在搜索：" + word);
                // cout << "用户在搜索：" << word << endl;
                string json_str;
                searcher.Search(word, &json_str);
                rsp.set_content(json_str, "application/json");
                // rsp.set_content("你好,世界！", "text/plain;charset=utf-8"); });
            });
    svr.listen("0.0.0.0", 8081); // 绑定任意ip 和 端口号
    return 0;

    // svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
    //     auto numbers = req.matches[1];
    //     res.set_content(numbers, "text/plain");
    // });

    // svr.Get("/stop", [&](const Request& req, Response& res) {
    //     svr.stop();//停止服务
    // });
}